package com.example.demo.leakybucket;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

@Component
public class LeakyBucketQueueService {
    //线程安全的队列
    private final BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(10);

    public BlockingQueue<Runnable> getQueue() {
        return queue;
    }

    @Scheduled(fixedRate = 1000)
    public void execute(){
        //每秒执行一次 = 每秒滴1滴水
        Runnable poll = queue.poll();
        if(poll != null){
            poll.run();
        }
    }
}
